package com.zx.dsptuneup_TriMic.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.example.base.tools.tools.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.gyf.barlibrary.ImmersionBar;
import com.zx.dsptuneup_TriMic.Adapter.MatrixAdapter;
import com.zx.dsptuneup_TriMic.Adapter.MatrixInputAdapter;
import com.zx.dsptuneup_TriMic.Adapter.MatrixOutputAdapter;
import com.zx.dsptuneup_TriMic.Base.BaseActivity;
import com.zx.dsptuneup_TriMic.Data_Bean.DevicesDetailInfoBean;
import com.zx.dsptuneup_TriMic.Data_Bean.MatrixVo;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.Tools.Constant;
import com.zx.dsptuneup_TriMic.Util.StringUtil;
import com.zx.dsptuneup_TriMic.Util.ToastUtil;
import com.zx.dsptuneup_TriMic.Util.UserUtil;
import com.zx.dsptuneup_TriMic.bean.BasicResponse;
import com.zx.dsptuneup_TriMic.bean.ChildDeviceBean;
import com.zx.dsptuneup_TriMic.network.DefaultObserver;
import com.zx.dsptuneup_TriMic.network.NetWorkManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MatrixActivity extends BaseActivity implements MatrixAdapter.OnDbChangeListener,MatrixInputAdapter.OnInputDbChangeListener,MatrixOutputAdapter.OnOutputDbChangeListener {
    @BindView(R.id.matrix_recycler)
    RecyclerView matrix_recycler;
    @BindView(R.id.ic_nav)
    TextView ic_nav;
    @BindView(R.id.input_rel_recyle)
    RecyclerView input_rel_recyle;
    @BindView(R.id.output_rel_recyle)
    RecyclerView output_rel_recyle;
    @BindView(R.id.output_hsv)
    HorizontalScrollView output_hsv;
    @BindView(R.id.matrix_hsv)
    HorizontalScrollView matrix_hsv;

    private int groupId = -1;
    private List<MatrixVo> matrixlist;
    private List<MatrixVo> matrixInputlist = new ArrayList<>();      // 输入 14组数据
    private List<MatrixVo> matrixOutputlist = new ArrayList<>();     // 输出 14组数据
    private List<MatrixVo> matrixcontentlist = new ArrayList<>();     // 中间 14*8 组数据

    private MatrixOutputAdapter matrixOutputAdapter;
    private MatrixInputAdapter matrixInputAdapter;
    private MatrixAdapter matrixAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matrix);
        Intent intent = getIntent();
        groupId = intent.getIntExtra(Constant.GroupID,0);

        initUI();
        String MatrixVoListStr = intent.getStringExtra(Constant.MatrixVoList);
        try {
            matrixlist = new Gson().fromJson(MatrixVoListStr,new TypeToken<List<MatrixVo>>(){}.getType());
            dealWithVolList(matrixlist);
//            Log.e("222",MatrixVoListStr);
        }catch (JsonSyntaxException e){
            e.printStackTrace();
            ToastUtil.makeText(MatrixActivity.this,"音频矩阵数据异常!");
            finish();
        }

    }

    protected void initUI() {
        ImmersionBar.with(this)
                .transparentBar()
                .statusBarColor(R.color.home_bg)
                .statusBarDarkFont(false)
                .init();
        ic_nav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        for (int i=0;i<112;i++){
            MatrixVo childDeviceBean = new MatrixVo();
            matrixcontentlist.add(childDeviceBean);
        }
        matrixAdapter = new MatrixAdapter(this, matrixcontentlist);
        matrix_recycler.setLayoutManager(new GridLayoutManager(this,8));
        matrix_recycler.setAdapter(matrixAdapter);
        matrixAdapter.setOnDbChangeListener(this);


        for (int i=0;i<14;i++){
            MatrixVo childDeviceBean = new MatrixVo();
            matrixInputlist.add(childDeviceBean);
        }
        matrixInputAdapter = new MatrixInputAdapter(this,matrixInputlist);
        input_rel_recyle.setLayoutManager(new LinearLayoutManager(this));
        input_rel_recyle.setAdapter(matrixInputAdapter);
        matrixInputAdapter.setOnDbChangeListener(this);

        for (int i=0;i<8;i++){
            MatrixVo childDeviceBean = new MatrixVo();
            matrixOutputlist.add(childDeviceBean);
        }
        matrixOutputAdapter = new MatrixOutputAdapter(this,matrixOutputlist);
        output_rel_recyle.setLayoutManager(new GridLayoutManager(this,8));
        output_rel_recyle.setAdapter(matrixOutputAdapter);
        matrixOutputAdapter.setOnOutputDbChangeListener(this);

        // 绑定recycleView联动
        input_rel_recyle.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    matrix_recycler.scrollBy(dx, dy);
                }
            }
        });
        matrix_hsv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
                output_hsv.scrollTo(i, i1);
            }
        });
        //  TODO  sdk>23
        output_hsv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
                matrix_hsv.scrollTo(i, i1);
            }
        });

        matrix_recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    input_rel_recyle.scrollBy(dx, dy);
//                    output_hsv.scrollTo(dx, dy);
                }
            }
        });
    }

    /**
     * 处理数据
     * */
    private void dealWithVolList(List<MatrixVo> matrixlist) {
        if(matrixlist == null || matrixlist.size() < 23){
            ToastUtil.makeText(MatrixActivity.this,"音频矩阵数据异常!");
            return;
        }
        matrixInputlist = matrixlist.subList(0,14);
        matrixOutputlist = matrixlist.subList(14,22);
        matrixcontentlist = matrixlist.subList(22,matrixlist.size());

//        Log.e("111","");
        matrixInputAdapter.setGridViewBeanList(matrixInputlist);
        matrixOutputAdapter.setGridViewBeanList(matrixOutputlist);
        matrixAdapter.setGridViewBeanList(matrixcontentlist);
    }


    @Override
    public void InputDbChange(String db, int position,int type) {
        if(StringUtils.isEmpty(db)){
            ToastUtil.makeText(MatrixActivity.this,"请输入db信息!");
            return;
        }
        if(Integer.parseInt(db)>30 || Integer.parseInt(db)<-72 ){
            ToastUtil.makeText(MatrixActivity.this,"请输入正确的范围!");
            return;
        }
        int outIndex = position/13;
        MatrixVo matrixVo = matrixInputlist.get(position);
        Log.e("1111","outIndex=="+outIndex);
        Log.e("1111","db==="+db+"  position=="+position);
        // http://localhost:8012/device/matrix?input=1&output=1&type=0&gain=45&group=1
        HashMap map = new HashMap();
        map.put("input",matrixVo.getInput());
        map.put("output",matrixVo.getOutput());
        map.put("type",type);
        map.put("gain",Integer.valueOf(db));
        map.put("group",groupId);
        setMatrixData(map);


    }

    @Override
    public void OutputDbChange(String db, int position,int type) {
        if(StringUtils.isEmpty(db)){
            ToastUtil.makeText(MatrixActivity.this,"请输入db信息!");
            return;
        }
        if(Integer.parseInt(db)>30 || Integer.parseInt(db)<-72 ){
            ToastUtil.makeText(MatrixActivity.this,"请输入正确的范围!");
            return;
        }
        int outIndex = position/13;
        MatrixVo matrixVo = matrixOutputlist.get(position);
        Log.e("1111","outIndex=="+outIndex);
        Log.e("1111","db==="+db+"  position=="+position);
        // http://localhost:8012/device/matrix?input=1&output=1&type=0&gain=45&group=1
        HashMap map = new HashMap();
        map.put("input",matrixVo.getInput());
        map.put("output",matrixVo.getOutput());
        map.put("type",type);
        map.put("gain",Integer.valueOf(db));
        map.put("group",groupId);
        setMatrixData(map);


    }

    @Override
    public void DbChange(String db, int position,int type) {
        if(StringUtils.isEmpty(db)){
            ToastUtil.makeText(MatrixActivity.this,"请输入db信息!");
            return;
        }
        if(Integer.parseInt(db)>30 || Integer.parseInt(db)<-72 ){
            ToastUtil.makeText(MatrixActivity.this,"请输入正确的范围!");
            return;
        }
        int outIndex = position/13;
        MatrixVo matrixVo = matrixcontentlist.get(position);
        Log.e("1111","outIndex=="+outIndex);
        Log.e("1111","db==="+db+"  position=="+position);
        // http://localhost:8012/device/matrix?input=1&output=1&type=0&gain=45&group=1
        HashMap map = new HashMap();
        map.put("input",matrixVo.getInput());
        map.put("output",matrixVo.getOutput());
        map.put("type",type);
        map.put("gain",Integer.valueOf(db));
        map.put("group",groupId);
        setMatrixData(map);

    }

    private void setMatrixData(HashMap map ) {
        NetWorkManager.getInstance().getNetWorkApiService().setMatrix(map).compose(MatrixActivity.this.<BasicResponse<DevicesDetailInfoBean>>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BasicResponse<DevicesDetailInfoBean>>() {
                    @Override
                    protected void onSuccess(BasicResponse<DevicesDetailInfoBean> response) {
                        if (response != null) {
                        }
                    }
                    @Override
                    protected void onFail(BasicResponse<DevicesDetailInfoBean> response) {
                        super.onFail(response);

                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
